
function init(){
	
	/**
	 * 发货数量校验
	 */
	$('.tbody-class').on('keyup','.send_quantity',function(){
		var deliveryVal = parseFloat($(this).val());
		var hadDeliveryVal = $(this).parent().prev().text();
		hadDeliveryVal = parseFloat(hadDeliveryVal.substr(0,hadDeliveryVal.length-1).trim());
		var orderVal = $(this).parent().prev().prev().text();
		orderVal = parseFloat(orderVal.substr(0,orderVal.length-1).trim());
		var warehouseVal = $(this).parent().prev().prev().prev().text();
		warehouseVal = parseFloat(warehouseVal.substr(0,warehouseVal.length-1).trim());
		// 判断当前发货数量是否大于库存数量
		if(deliveryVal > warehouseVal){
			layer.msg('发货数量大于库存数量！',{icon:5,time:1000});
			$(this).val(0);
		}else{
			// 判断当前发货数量和已发货数量是否大于订单数量
			if((deliveryVal + hadDeliveryVal) > orderVal){
				layer.msg('发货数量大于订单！',{icon:5,time:1000});
				$(this).val(0);
			}
		}
		
	})
	
	/**
	 * 点击提交按钮后创建发货单
	 */
	$('.top-title').on('click','.send',function(){
		var orderId = GetQueryString('id');// 获取传递过来的订单ID
		var jsonData = '[';
		var num = 0;
		// 获取所有订单ID、商品价格ID和商品数量
		$('.send_quantity').each(function(){
			if($(this).val() > 0){
				jsonData += '{"productPriceId": "' + $(this).attr('data-product-price') +  '", "quantity": "' + $(this).val() + '"},';
				num++;
			}
		});
		jsonData = jsonData.substring(0,jsonData.length-1);
		jsonData += "]";
		if(num > 0){
			// 创建发货单
			$.ajax({
				type:"POST",
				url:path +"/delivery/saveDelivery",
				data:{jsonData:jsonData, orderId: orderId},
				success:function(data){
					window.location.href = path +"/distributor/delivery_bill";
				},
				error:function(data){
					layer.msg('创建发货单失败！',{icon:5,time:1000});
				}
			});
		}else{
			layer.msg('请填写发货数量！',{icon:5,time:1000});
		}
	});
	
	loadComment();
}

/**
 * 加载商品列表
 */
function loadComment(){
	var id = GetQueryString('id');// 获取传递过来的订单ID
	$.ajax({
		type:"POST",
		url:path +"/orderlang/orderProduct/orderProductList",
		data:{orderId:id},
		success:function(data){
			if(data.state == 1){
				var list = data.data;
				var html = "";
				for(var i = 0;i<list.length;i++){
					html += "<tr>";
					html += "<td><img src='"+list[i].imgHref+"' alt='''/></td>";
					html += "<td>"+list[i].productName+"</td>";
					html += "<td>";
					for(var j = 0;j<list[i].sepcList.length;j++){
						html += "<span>"+list[i].sepcList[j].specName+":"+list[i].sepcList[j].specValue+"</span>；";
					}
					html += "</td>";
					html += "<td>"+list[i].quantity+"&nbsp;&nbsp;"+list[i].unit+"</td>";
					html += "<td>"+list[i].orderQuantity+"&nbsp;&nbsp;"+list[i].unit+"</td>";
					html += "<td>"+list[i].deliveryQuantity+"&nbsp;&nbsp;"+list[i].unit+"</td>";
					html += "<td>";
					html += "<input type='text' data-order='"+id+"' data-product-price='"+list[i].productPriceId+"' class='send_quantity' value='0'/><span>&nbsp;&nbsp;"+list[i].unit+"</span>";
					html += "</td>";
					html += "<td></td>";
					html += "</tr>";
					$('.top-title>p:first-child').html('<span>订单号：</span>'+list[i].orderCode);
					$('.top-title>p:first-child').next().html('<span>客户名称：</span>'+list[i].customerName);
				}
				$('.detail-block').find('tbody').html(html);
			}
		},
		error: function(){
			layer.msg('加载列表失败！',{icon:5,time:1000});
		}
	});
}
function GetQueryString(name){
     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]);
     return null;
}

init();
